<?php	
// Utilities.php has some small utility functions to be used
  include("Utilities.php");
// DBConn.php helps to connect to dtabase easily
  include("Includes/DBConn.php");

// This function renders an editable Linear gauge chart 
// This acts as year selector control
  
  function showYearSelector($intYear){
	$link=connectToDB();
	$lowerLimit=0; $upperLimit=0; $strCR=0; $thisYear=0; $counter=0;
	$dataXML=""; $strStyles=""; $strAnn="";

	//Retrieve the years
	
	$strSQL = "SELECT DISTINCT YEAR(OrderDate) As Year FROM FC_Orders ORDER BY 1";
	$result = mysql_query($strSQL) or die(mysql_error());
	
	// Render them in drop down box	
	$strCR= "";
	
	$counter = 0;
	// iterate through years
	while($oRs= mysql_fetch_array($result)){
	// store the year from recordset
		$thisYear=$oRs['Year'];
		// sets the first year retrieved as linear gauge's lowerlimit
		if( $counter==0){ $lowerLimit = $thisYear; }
		// Create annotations texts for linear gauge using retrieved years

		  $strCR .= "<annotation type='text'  x='" . (65+(90*$counter)) . "' y='30' font='Georgia' size='22' isBold='0' alpha='10' color='FFFFFF' label='" . $thisYear . "' />";
		
		$counter++;
	}
	// sets upper limit of the linear gauge as the last retrieved year
	$upperLimit = $thisYear;
	// open chart element of the linear gauge XML
	$dataXML ="<chart borderAlpha='0' animation='0' showToolTip='0' editMode='1' lowerLimit='" . (floor($lowerLimit)-.50) . "' upperLimit='" . (floor($upperLimit)+.49) . "' gaugeRoundRadius='0'  showTickMarks='0'  showGaugeLabels='1' pointerOnTop='1' pointerRadius='9' pointerBgAlpha='80' pointerBorderThickness='1' pointerBorderColor='aa0000' pointerBgColor='cc0000' showToolTip='1' decimals='0' showLimits='1' showValue='0' formatNumber='0' formatNumberScale='0' decimals='0'>";
	
	// build annotation XML element to load year selector background image
	// it also adds the years annotattion texts created above
    $strAnn ="<annotations><annotationGroup id='Grp1' showBelow='1'><annotation type='image' x='0' y='0' URL='Images/YearPanel.jpg'/></annotationGroup><annotationGroup id='AnnYear' showBelow='0'>" . $strCR . "</annotationGroup></annotations>";
	
	// build style XML element
	$strStyles ="<styles><definition><style name='Font1' type='font' face='Georgia' size='22' color='FFFFFF' bold='0' /><style name='Shadow1' type='Shadow' color='000000' alpha='50' Distance='2' /></definition><application><apply toObject='AnnYear' styles='Font1,Shadow1' /></application></styles>";
	
	// set the default year value
	// if its  beyond lower/upper limits we set it to upper limit
	if( $intYear<$loweLimit and $intYear>$upperLimit){ $intYear=$upperLimit; } 
	
	// set the chart pointer value to dafault year & close chart element
	$dataXML .=  "<value>" . $intYear . "</value>" . $strAnn . $strStyles . "</chart>";
	
	// render linear gauge using the dataXML created above
	print renderChart("Charts/HLinearGauge.swf", "",$dataXML, "FG_YearSelector" , 303,50, false, true);
	
	mysql_close($link);
	
 }

// This function renders a Combination column 3d line dual y axis chart to show sales of 3 years
 function showAllYearsData(){
	// create dataURL
	$dataURL=urlencode("DataGen.php?op=buildXMLAllYearsData");
	// render chart
	print renderChart("Charts/MSColumn3DLineDY.swf", $dataURL, "", "FC_AllYearsData" , 260,250, false, true);
	
}

// This function renders an angual chart to show average revenue
// @param 	intYear		Selected year
function showAvgSalesRev($intYear){
	// Create dataURL 
	$dataURL=urlencode("DataGen.php?op=buildXMLAvgRev&year=" . $intYear);
	// render chart
	print renderChart("Charts/AngularGauge.swf", $dataURL, "", "FG_AvgRev" , 192,118, false, true);
 }

// This function creates an angular chart to show average sales qunatity
// @param 	intYear		Selected year
 function showAvgSalesQuantity($intYear){
	// Create dataURL 
	$dataURL=urlencode("../chart/ybp.php");
	// render chart
	print renderChart("../chart/Charts/AngularGauge.swf", $dataURL, "", "FG_AvgQuan" , 198,118, false, true);
	
 }

// This function creates a spark column chart to show current year's monthly revenue
// @param 	intYear		Selected year
 function showMonthlyRev($intYear){
	// Create dataURL 
	$dataURL=urlencode("DataGen.php?op=buildXMLMonthlyRev&year=" . $intYear);
	// render chart
	print renderChart("Charts/SparkColumn.swf", $dataURL, "", "FG_MonthlyRev" , 190,40, false, true);
 }

// This function creates a spark column chart to show current year's monthly qunatity sales
// @param 	intYear		Selected year
 function showMonthlyQuan($intYear){
	// Create dataURL 
	$dataURL=urlencode("DataGen.php?op=buildXMLMonthlyQuan&year=" . $intYear);
	// render chart
	print renderChart("Charts/SparkColumn.swf", $dataURL, "", "FG_MonthlyQuan" , 190,40, false, true);
 }

// This function creates a Funnel chart to show current year's top 5 countries(ranked as per revenue)
// @param 	intYear		Selected year
 function showTopCountries($intYear){
	// Create dataURL 
	$dataURL=urlencode("DataGen.php?op=buildXMLTopCountries&year=" . $intYear);
	// render chart
	print renderChart("Charts/Funnel.swf", $dataURL, "", "FG_TopCountries" , 335,270, false, true);
 }
 
// This function creates a Pyramid chart to show current year's top 10 employees (ranked as per revenue)
// @param 	intYear		Selected year
 function showTopEmployees($intYear){
 	// Create dataURL 
	$dataURL=urlencode("DataGen.php?op=buildXMLTopEmployees&year=" . $intYear);
	// render chart
	print renderChart("Charts/Pyramid.swf", $dataURL, "", "FG_TopEmployees" , 335,270, false, true);
 } 
 //----------------------------------------------------------------------------------------------------------
 
// This function creates 5  cylinder charts to show inventories of top 5 products
// top 5 products are ranked by their qantity sold that year
// @param 	intYear		Selected year
 function showTopProductInventories($intYear){
 	// inltialize variables
	// counter stores the rank values of each chart & helps in creating each chart's domID
  $dataURL=""; $counter=0;
	// iterate though each rank
  for($counter=1; $counter<6; $counter++){
  	// create dataURL for each ranked product's cylinder 
		$dataURL=urlencode("DataGen.php?op=buildXMLEachInventories&rank=" . $counter . "&year=". $intYear);
		// create a table column
		print "<td>";
		// render cylinder chart 
		print renderChart("Charts/Cylinder.swf", $dataURL,"", "FG_PI" . $counter , 140,250, false, true);
		// close table column
		print "</td>";
	}
 }

 // ''''''''''''''''''''''''''''''''''''''''''''''''''''

// This function creates a combination olumn3d line dual axis chart to show 
// top 10 customers (ranked as per revenue) 
// @param 	intYear		Selected year
 function showTopCustomers($intYear){
 	// create dataURL
	$dataURL=urlencode("DataGen.php?op=buildXMLTopCustomers&year=" . $intYear);
	// render chart
	print renderChart("Charts/MSColumn3DLineDY.swf", $dataURL, "", "FC_TopCustomers" , 714,250, false, true);
 }

 //----------------------------------------------------------------------------------------------------------

?>